package com.rebate.web.servlet;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.rebate.model.RebateDetail;
import com.rebate.service.IBaseService;

@Controller
public class RebateDetailServlet extends BaseServlet{
	
	private String view = "/rebatedetail/listdetail";
	private String editView = "/rebatedetail/editdetail";
	private String createView = "/rebatedetail/createdetail";
	@Autowired
	private IBaseService<RebateDetail> rebateDetailService;
	
	/**
	 * 商户返利详情列表页面
	 * @throws IOException 
	 */
	@RequestMapping(value = "/rebatedetail/list.htm")
	public String getDetailByMerchant(HttpServletRequest request,ModelMap model,
			HttpServletResponse response,@ModelAttribute("rebateDetail") RebateDetail rebateDetail) throws IOException {
		List<RebateDetail> list = rebateDetailService.findBySelective(rebateDetail);
		model.put("detailList", list);
		return view;
	}
	
	/**
	 * init create page
	 * @throws IOException 
	 */
	@RequestMapping(value = "/rebatedetail/add.htm")
	public String addInit(HttpServletRequest request,ModelMap model,
			HttpServletResponse response,@ModelAttribute("rebateDetail") RebateDetail rebateDetail) throws IOException {
		String merchantId = request.getParameter("merchantId");
		model.put("merchantId", merchantId);
		return createView;
	}
	
	/**
	 * save merchant detail
	 * @throws IOException 
	 */
	@RequestMapping(value = "/rebatedetail/save.htm")
	public void save(HttpServletRequest request,ModelMap model,
			HttpServletResponse response,@ModelAttribute("rebateDetail") RebateDetail rebateDetail) throws IOException {
		rebateDetail.setStartDate(parseDate(request.getParameter("startDateStr")));
		rebateDetail.setEndDate(parseDate(request.getParameter("endDateStr")));
		rebateDetail.setCreateTime(new Date());
		Long id = rebateDetailService.create(rebateDetail);
		if (id != null) {
			response.getWriter().print(1);//successful
		}else {
			response.getWriter().print(0);
		}
	}
	
	/**
	 * edit detail page
	 * @throws IOException 
	 */
	@RequestMapping(value = "/rebatedetail/edit.htm")
	public String editInit(HttpServletRequest request,ModelMap model,
			HttpServletResponse response,@ModelAttribute("rebateDetail") RebateDetail rebateDetail) throws IOException {
		rebateDetail = rebateDetailService.findById(rebateDetail.getId());
		model.put("rebateDetail", rebateDetail);
		return editView;
	}
	
	/**
	 * update detail
	 * @throws IOException 
	 */
	@RequestMapping(value = "/rebatedetail/update.htm")
	public void update(HttpServletRequest request,ModelMap model,
			HttpServletResponse response,@ModelAttribute("rebateDetail") RebateDetail rebateDetail) throws IOException {
		rebateDetail.setStartDate(parseDate(request.getParameter("startDateStr")));
		rebateDetail.setEndDate(parseDate(request.getParameter("endDateStr")));
		rebateDetail.setUpdateTime(new Date());
		if (rebateDetailService.update(rebateDetail)) {
			response.getWriter().print(1);
		}else {
			response.getWriter().print(0);
		}
	}
	
}
